package com.example.landlord.demo.Service.Impl;

import com.example.landlord.demo.Controller.dto.LoginDTO;
import com.example.landlord.demo.Controller.dto.RegisterDTO;
import com.example.landlord.demo.Controller.request.ForgetRequest;
import com.example.landlord.demo.Controller.request.LoginRequest;
import com.example.landlord.demo.Controller.request.RegisterRequest;
import com.example.landlord.demo.Controller.request.UserPageRequest;
import com.example.landlord.demo.Entity.User;
import com.example.landlord.demo.Exception.ServiceException;
import com.example.landlord.demo.Mapper.UserMapper;
import com.example.landlord.demo.Service.ISendEmailService;
import com.example.landlord.demo.Service.IUserService;
import com.example.landlord.demo.total.TotalV;
import com.example.landlord.demo.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UserService implements IUserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    ISendEmailService sendEmailService;

    @Override
    public LoginDTO login(LoginRequest request) {
        User user = userMapper.getByUsernameAndPassword(request);
        if (user == null) {
            throw new ServiceException("用户名或密码错误");
        }
        LoginDTO loginDTO = new LoginDTO();
        //把user类里面名字相同的复制到loginDTO中
        BeanUtils.copyProperties(user, loginDTO);


        // 生成token
        String token = TokenUtils.genToken(String.valueOf(user.getId()), user.getUPwd());
        loginDTO.setToken(token);

        return loginDTO;
    }

    @Override
    public List<User> list() {
        return userMapper.list();
    }


    //查找数据库中是否已存在用户名
    public RegisterDTO findUname(RegisterRequest request) {
        User user = userMapper.findUname(request);
        if (user == null) {
            return null;
        }
        RegisterDTO registerDTO = new RegisterDTO();
        BeanUtils.copyProperties(user, registerDTO);
        return registerDTO;
    }

    @Override
    public void register(RegisterRequest request) {
        System.out.println("verificationCodes" + TotalV.verificationCodes.get(request.getUName()));
        System.out.println("verificationCodes" + TotalV.verificationCodes.get(request.getUName()));
        if (request.getVerificationCode() == null || TotalV.verificationCodes.get(request.getUName()) == null ||
                !request.getVerificationCode().equals(TotalV.verificationCodes.get(request.getUName()))) {
            throw new ServiceException("请输入正确的验证码");
        }
        if (findUname(request) != null) {
            throw new ServiceException("该用户名已存在，请重新输入");
        } else {
            userMapper.insert(request);
        }
    }

    @Override
    public void forget(ForgetRequest request) {
        User user = userMapper.ByUsernameGetEmail(request);
        System.out.println(request.getEmail());
        System.out.println(user.getEmail());
        if (!(request.getEmail().equals(user.getEmail()))) {
            throw new ServiceException("用户名或邮箱不正确");
        }
        sendEmailService.sendPassword(request);
    }

    @Override
    public void change(User request) {
        User user = userMapper.ByUsernamePassword(request);
        if(request.getNewUpwd()== null){
            throw new ServiceException("新密码不能为空");
        }
        if (!request.getUPwd().equals(user.getUPwd())) {
            throw new ServiceException("你输入的密码有误!");
        }
        userMapper.updatePassword(request);
    }

    @Override
    public User getById(Integer id) {
        User user = userMapper.getById(id);
        return user;
    }

    @Override
    public User getByuname(String uname) {
        User user =userMapper.getByuname(uname);

        return user;
    }

    @Override
    public void updateP(User user) {
        userMapper.updateP(user);
    }

    @Override
    public void addScore(String uname, int score) {
        userMapper.addScore(uname,score);
    }

//    @Override
//    public PageInfo<User> page(UserPageRequest baseRequest) {
//        PageHelper.startPage(baseRequest.getPageNum(), baseRequest.getPageSize());
//        List<User> users = userMapper.listByCondition(baseRequest);
//        return new PageInfo<>(users);
//    }

}
